<?php

class Publish_ArticleController extends Ext_Controller_Action_Front
{
    private $_userId;
    
    public function init()
    {
        parent::init();
    }
    
    public function preDispatch() {
        parent::preDispatch();
    }
    
    public function deleteAction()
    {
        $viewResult = new Ext_View_Result();
        
        $articleId      = $this->filter->filter($this->_request->get('articleId'));
        
        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_Delete_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $delete = new Add_Publish_Article_Delete($articleId);
        $delete->delete();
        
        $viewResult->setMessage($this->translater->_("Delete success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function savePostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $articleId      = $this->filter->filter($this->_request->get('articleId'));
        $title          = $this->filter->filter($this->_request->getPost('title'));
        $video          = $this->_request->getPost('video');
        $image          = $this->_request->getPost('image');
        $music          = $this->_request->getPost('music');
        $shopping       = $this->_request->getPost('shopping');
        $description    = $this->_request->getPost('description');
        $recommendTags  = $this->_request->getPost('recommendTags');
        $definedTags    = $this->filter->filter($this->_request->getPost('definedTags'));
        //Zend_Debug::dump($description, '$description');
        
        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Acl($articleId))
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Title($articleId, $title))
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Description($articleId, $description))
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Attachment_Video($articleId, $video))
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Attachment_Image($articleId, $image))
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Attachment_Music($articleId, $music))
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Attachment_Shopping($articleId, $shopping))
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Tags($articleId, $recommendTags, $definedTags))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $data = new Add_Publish_Article_SavePost_Data();
        $data->setTitle($title);
        $data->setDescription($description);
        $data->setImage($image);
        $data->setVideo($video);
        $data->setMusic($music);
        $data->setShopping($shopping);
        $data->setDefinedTags($definedTags);
        $data->setRecommendTags($recommendTags);
        
        $savePostor = new Add_Publish_Article_SavePost($articleId, $data);
        $savePostor->save();
        
        $viewResult->addMessage($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();

    }

    public function showFormAction()
    {
        $articleId         = $this->filter->filter($this->_request->get('articleId'));

        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_ShowForm_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult = new Ext_View_Result();
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $showForm = new Add_Publish_Article_ShowForm($articleId);

        $config = Ext_Config::getInstance()->getConfig("attachment");
        $attachmentConfig = $config->toArray();
        if(!is_array($attachmentConfig)){
            $attachmentConfig = array();
        }
        if(!is_array($attachmentConfig['video'])){
            $attachmentConfig['video'] = array();
        }
        $allowVideoHosts = array();
        foreach($attachmentConfig['video'] as $hostKey=>$hostVal)
        {
            if(!isset($hostVal['host']) || !isset($hostVal['name']) || !isset($hostVal['url'])){
                continue;
            }
            
            $allowVideoHosts[] = array("name"=>$hostVal['name'], "url"=>$hostVal['url']);
        }
        $this->view->allowVideoHosts = $allowVideoHosts;
        if(!is_array($attachmentConfig['music'])){
            $attachmentConfig['music'] = array();
        }
        $allowMusicHosts = array();
        foreach($attachmentConfig['music'] as $hostKey=>$hostVal)
        {
            if(!isset($hostVal['host']) || !isset($hostVal['name']) || !isset($hostVal['url'])){
                continue;
            }
            
            $allowMusicHosts[] = array("name"=>$hostVal['name'], "url"=>$hostVal['url']);
        }
        $this->view->allowMusicHosts = $allowMusicHosts;
        if(!is_array($attachmentConfig['shopping'])){
            $attachmentConfig['shopping'] = array();
        }
        $allowShoppingHosts = array();
        foreach($attachmentConfig['shopping'] as $hostKey=>$hostVal)
        {
            if(!isset($hostVal['host']) || !isset($hostVal['name']) || !isset($hostVal['url'])){
                continue;
            }
            
            $allowShoppingHosts[] = array("name"=>$hostVal['name'], "url"=>$hostVal['url']);
        }
        $this->view->allowShoppingHosts = $allowShoppingHosts;
        
        $this->view->articleId        = $showForm->getArticleId();
        $this->view->title            = $showForm->buildTitle();
        $this->view->attachments      = $showForm->buildAttachments();
        $this->view->description      = $showForm->buildDescription();
        $this->view->recommendTags    = $showForm->buildRecommendTags();
        $this->view->definedTags      = $showForm->buildDefinedTags();
        
        //print_r($this->view->attachments);
    }
    
}

?>